package com.kws.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.common.core.domain.R;
import com.kws.system.domain.TbDeviceRemark;
import com.kws.system.domain.info.QueryReviewManagementInfo;
import com.kws.system.domain.vo.QueryReviewManagementRq;
import com.kws.system.mapper.ReviewManagementMapper;
import com.kws.system.service.ReviewManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <b>ReviewManagementServiceImpl</b>
 *
 * @description: ReviewManagementServiceImpl <br>
 * @date: 2024/11/19 08:59 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class ReviewManagementServiceImpl extends ServiceImpl<ReviewManagementMapper, TbDeviceRemark> implements ReviewManagementService {
    private static final Logger logger = LoggerFactory.getLogger(ReviewManagementServiceImpl.class);

    @Override
    public R<Page<QueryReviewManagementInfo>> queryReviewPage(QueryReviewManagementRq rq) {
        Page<QueryReviewManagementInfo> page = new Page<>(rq.getPageNum(), rq.getPageSize());
        Page<QueryReviewManagementInfo> pageInfo = baseMapper.queryReviewPage(page,rq);
        if (CollectionUtil.isNotEmpty(pageInfo.getRecords())){
            List<QueryReviewManagementInfo> records = pageInfo.getRecords();
            records.forEach(item -> {
                rq.setId(item.getId());
                List<QueryReviewManagementInfo> reviewManagementList = baseMapper.queryReviewList(rq);
                item.setReviewList(reviewManagementList);
            });
            pageInfo.setRecords(records);
        }
        return R.ok(pageInfo);
    }

    @Override
    public List<QueryReviewManagementInfo> queryReviewById(Long id) {
        List<QueryReviewManagementInfo> list = baseMapper.queryReviewById(id);
        if (CollectionUtil.isNotEmpty(list)){
            list.forEach(item -> {
                //统计订单价格
                Double totalOrderPrice = baseMapper.countOrderPrice(item.getOrderNo());
                if (null != totalOrderPrice) {
                    item.setOrderPrice(new BigDecimal(totalOrderPrice));
                }else {
                    item.setOrderPrice(BigDecimal.ZERO);
                }
            });
        }
        return list;
    }

    @Override
    public boolean deleteReviewById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }
}
